package com.starlight.easybuyprictice.entity;

import java.util.UUID;

/**
 * @Author yfy2001
 * @date 2022/11/28 22:14
 */
public class Token {
    public enum Type {
        TT_USER("普通用户"),
        TT_ADMIN("管理员"),
        TT_DEVELOP("开发者"),
        TT_DEVICE("设备");
        private String name;
        Type(String name){
            this.name = name;
        }
        public String getName(){
            return name;
        }
    }
    private long id;
    private Type type;
    private String uuid;
    public Token(String value) {
        String[] parameters = value.split("-");
        if (parameters.length != 3) {
            throw new IllegalArgumentException( "invalid token value ");
        }
        id = Long.parseLong(parameters[0]);
        type = Type.values()[Integer.parseInt(parameters[1])];
        uuid = parameters[2];
    }
    public Token(long id,Type type) {
        this(id,type, UUID.randomUUID().toString().replace( "-", ""));
    }
    public Token(long id,Type type,String uuid){
        this.id = id;
        this.type = type;
        this.uuid = uuid;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        this.type = type;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getValue() {
        return String.valueOf(id) + '-' + type.ordinal() + '-' + uuid;
    }

    @Override
    public String toString() {
        return "Token{" +
                "id=" + id +
                ", type=" + type +
                ", uuid='" + uuid + '\'' +
                '}';
    }
}
